/**
 * 
 */
package com.ms.game.itembox.container;

import com.ms.game.itembox.Item;
import com.ms.game.itembox.message.SUseSilentCardNotify;
import com.ms.player.Player;
import com.ms.player.PlayerAttributeType;
import com.ms.player.PlayerNickNameParser;
import com.ms.player.UpdateAttributeList;
import com.ms.service.error.ErrorCode;
import com.ms.service.module.zone.module.BasicZoneModule;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.game.zone.PlayerDataAgentZone;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.group.transportlayer.GroupMessage;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.common.PlayerCommonInfoManager;

/**
 * 禁言卡
 * 
 * @author TW15010
 */
public class ItemSilentCard extends ItemBasic {
	/** 持續時間 (秒) */
	private int durationSec;

	public int getDurationSec() {
		return durationSec;
	}

	public void setDurationSec(int durationSec) {
		this.durationSec = durationSec;
	}

	// ServerCore only ====================================================

	@Override
	public int use(Player player, Item useItem, int useCount, GUID targetGuid, Object... object) {
		if (player == null) {
			return ErrorCode.INVALID_PARAM;
		}

		if (targetGuid == null) {
			return ErrorCode.INVALID_PARAM;
		}

		// 確認目標在線上
		String targetZoneId = PlayerCommonInfoManager.getInstance().getPlayerZoneId(targetGuid);
		if (targetZoneId == null) {
			return ErrorCode.USE_ITEM_TARGET_IS_NOT_ONLINE;
		}
		
		String sourcePlayerName = PlayerNickNameParser.mergeNickName(player.getName(), player.getNameNumber());

		// 目標跟使用的人在同一個 zone
		if (targetZoneId.equals(player.getZoneId())) {
			// 直接執行
			applyEffect(targetZoneId, sourcePlayerName, targetGuid);
		} else {
			// 通知那個 zone
			SUseSilentCardNotify data = new SUseSilentCardNotify();
			data.setItemId(getItemId());
			data.setZoneId(targetZoneId);
			data.setSourcePlayerName(sourcePlayerName);
			data.setTargetGuid(targetGuid);

			GroupMessage msg = new GroupMessage();
			msg.setMessageType(MessageTypes.S_ZONE_USE_SILENT_CARD_NOTIFY);
			msg.setMessageContent(data);
			msg.setDestModuleID(targetZoneId);
			MessageAction.sendGroupMessage(msg);
		}

		return ErrorCode.SUCCESS;
	}

	/**
	 * zone 收到對某人使用禁言卡的通知後呼叫這個
	 * 
	 * @param zoneId
	 *            目標所在 zone id
	 * @param sourcePlayerName
	 *            來源使用者的名字
	 * @param targetGuid
	 *            目標玩家 guid
	 * @return 成功: true, 失敗: false
	 */
	public boolean applyEffect(String zoneId, String sourcePlayerName, GUID targetGuid) {
		// 取得目標 player 所在的 zone
		BasicZoneModule zoneModule = GameUtility.getZoneModule(zoneId);
		if (zoneModule == null) {
			return false;
		}

		// 取得目標 player
		Player targetPlayer = GameUtility.getPlayerFromZone(zoneId, targetGuid);
		if (targetPlayer == null) {
			return false;
		}

		// 通知目標 player 被使用了
		// 只是通知 client 被誰使用了，所以 zone id, target guid 都不填
		SUseSilentCardNotify data = new SUseSilentCardNotify();
		data.setItemId(getDefaultItemId());
		data.setZoneId("");
		data.setSourcePlayerName(sourcePlayerName);
		data.setTargetGuid(null);

		GUID session = PlayerCommonInfoManager.getInstance().getSessionByPlayerGuid(targetGuid);
		if (session != null) {
			MessageAction.sendMessage(session, MessageTypes.S_ZONE_USE_SILENT_CARD_NOTIFY, data);
		}

		// 更新 player 資料
		targetPlayer.setSilentDuration(getDurationSec());
		targetPlayer.sendUpdatedAttrToClient();
		
		UpdateAttributeList updateList = new UpdateAttributeList();
		updateList.addUpdateValue(PlayerAttributeType.SILENT_EXPIRE_TIME, targetPlayer.getSilentExpireTime());

		// 變更送回 db 儲存
		PlayerDataAgentZone playerDataAgentZone = zoneModule.getPlayerDataAgent();
		playerDataAgentZone.updatePlayerDataToDC2(targetPlayer, updateList, null, null);
		return true;
	}

	@Override
	public int applyItemToDc(BasicZoneModule zoneModule, Player player, Item useItem, GUID orderId, Object... object) {
		return 0;
	}
}
